﻿namespace HelloWorld.ViewModels
{
    using System.Reactive.Linq;
    using Codeplex.KinkumaFramework;
    using Codeplex.KinkumaFramework.Interactivity;
    using Codeplex.Reactive;
    using Codeplex.KinkumaFramework.Utils;
    using System.ComponentModel.DataAnnotations;

    public class MainPageViewModel
    {
        // KinkumaFramework
        public readonly ReactiveErrorsContainer errorsContainer;

        // ReactiveProperty
        [Display(Name = "Input1", Description = "Helloと入力してね")]
        public ReactiveProperty<string> Input1 { get; private set; }
        [Display(Name = "Input2", Description = "worldと入力してね")]
        public ReactiveProperty<string> Input2 { get; private set; }
        public ReactiveCommand ShowWindowCommand { get; private set; }

        // KinkumaFramework
        public ReactiveInteractionRequest<MessageBoxConfirmation> MessageBoxRequest { get; private set; }

        public MainPageViewModel()
        {
            if (DesignTimeUtils.IsInDesignMode)
            {
                return;
            }

            this.Input1 = new ReactiveProperty<string>()
                .SetValidateNotifyError(input => input.Select(s => s == "Hello" ? null : new[]{"Helloと入力してね"}));
            this.Input2 = new ReactiveProperty<string>()
                .SetValidateNotifyError(input => input.Select(s => s == "world" ? null : new[]{"worldと入力してね"}));

            // Container ReactiveProperty error.
            this.errorsContainer = new ReactiveErrorsContainer(
                this.Input1.ObserveErrorChanged,
                this.Input2.ObserveErrorChanged);

            // Create ReactiveCommand from ReactiveErrorsContainer.
            this.ShowWindowCommand = this.errorsContainer.ToReactiveCommand(false);

            // Create ReactiveInteractionRequest<Notification> from IO<Notification>.
            this.MessageBoxRequest =
                this.ShowWindowCommand
                    .Select(_ => new MessageBoxConfirmation
                    {
                        Title = "確認",
                        Content = this.Input1.Value + " " + this.Input2.Value
                    })
                    .ToReactiveInteractionRequest();
        }
    }
}
